<template>
  <el-card>
    <!-- 头部组件 -->
    <PageTools :total="total">
      <template #left>
        <el-form ref="form" :model="queryData" inline label-width="80px">
          <el-form-item label="关键字">
            <el-input
              v-model="queryData.keyword"
              placeholder="根据文章标题搜索"
            />
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="queryData.state" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button size="mini" @click="onSubmit">清除</el-button>
            <el-button
              size="mini"
              type="primary"
              @click="initData"
            >搜索</el-button>
          </el-form-item>
        </el-form>
      </template>
      <template #right>
        <el-button
          size="mini"
          type="success"
          icon="el-icon-edit"
          style="height: 100%"
          @click="addHandle"
        >新增标签</el-button>
      </template>
    </PageTools>
    <!-- 表格 -->
    <el-table :data="list" style="width: 100%">
      <el-table-column type="index" label="序号" />
      <el-table-column prop="title" label="文章标题">
        <template slot-scope="{ row }">
          {{ row.title }}
          <svg-icon v-if="row.videoURL" icon-class="eye" @click.native="showVideo(row.videoURL)" />
        </template>
      </el-table-column>
      <el-table-column prop="visits" label="阅读数" />
      <el-table-column prop="username" label="录入人" />
      <el-table-column prop="createTime" label="录入时间" />
      <el-table-column prop="state" label="状态" :formatter="formatState" />
      <el-table-column label="操作">
        <template slot-scope="{ row }">
          <el-button type="text" @click="preview(row)">预览</el-button>
          <el-button type="text" @click="editState(row)">{{
            row.state ? "禁用" : "启用"
          }}</el-button>
          <el-button
            :disabled="Boolean(row.state)"
            type="text"
            @click="editHandle(row.id)"
          >修改</el-button>
          <el-button type="text" @click="delHandle(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-row type="flex" justify="end">
      <el-pagination
        background
        :current-page="queryData.page"
        :page-sizes="[5, 12, 15, 20]"
        :page-size="5"
        :total="total"
        layout="prev, pager, next, sizes, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-row>
    <!-- 预览弹窗 -->
    <preview ref="preview" />
    <!-- 添加，修改弹窗 -->
    <add-article ref="addArticle" @update="initData" />
    <!-- 视频 -->
    <Video ref="video" />
  </el-card>
</template>

<script>
// 组件
import PageTools from '@/components/PageTools'
// 接口
import { getArticles, editArticleState, delArticle } from '@/api/articles'
import Preview from './components/preview.vue'
import AddArticle from './components/addArticle.vue'
import Video from './components/video.vue'
export default {
  components: {
    PageTools,
    Preview,
    AddArticle,
    Video
  },
  data() {
    return {
      list: [],
      total: 10,
      queryData: {
        page: 1,
        pagesize: 5,
        keyword: '',
        state: null
      },
      options: [
        { label: '启用', value: 1 },
        { label: '禁用', value: 0 }
      ]
    }
  },
  created() {
    this.initData()
  },
  methods: {
    handleSizeChange(val) {
      this.queryData.pagesize = val
      this.initData()
    },
    handleCurrentChange(val) {
      this.queryData.page = val
      this.initData()
    },
    // 初始化数据
    async initData() {
      const { counts, items } = await getArticles(this.queryData)
      this.total = counts
      // 格式化时间样式
      items.map((item) => {
        item.createTime = new Date(item.createTime).toLocaleString()
        return item
      })
      this.list = items
    },
    // 清除按钮
    onSubmit() {
      this.queryData = { ...this.queryData, keyword: '', state: null }
      this.initData()
    },
    // 预览
    preview(val) {
      this.$refs.preview.dialogVisible = true
      this.$refs.preview.item = val
    },
    // 修改状态
    async editState(val) {
      try {
        await editArticleState({ id: val.id, state: Number(!val.state) })
        this.initData()
        this.$message.success('状态修改成功')
      } catch (error) {
        this.$message.success(error)
      }
    },
    // 删除
    async delHandle(id) {
      await this.$confirm('确定删除吗')
      await delArticle(id)
      this.$message.success('删除成功')
      if (this.queryData.page !== 1 && this.list.length === 1) {
        this.queryData.page--
      }
      this.initData()
    },
    // 添加
    addHandle() {
      this.$refs.addArticle.dialogVisible = true
    },
    // 修改文章
    editHandle(id) {
      this.$refs.addArticle.dialogVisible = true
      this.$refs.addArticle.getArticle(id)
    },
    // 格式化状态
    formatState(row, column, cellValue, index) {
      return cellValue ? '已启用' : '已禁用'
    },
    // 显示视频
    showVideo(url) {
      this.$refs.video.flag = true
      this.$refs.video.url = url
    }
  }
}
</script>

<style scope>
.svg-icon {
  cursor: pointer;
}
</style>
